package com.smart.car.resource.controller;

import cn.hutool.core.date.DateUtil;
import com.aliyun.oss.OSSClient;
import com.smart.car.common.res.bean.ResponseResult;
import com.smart.car.common.res.constant.Constants;
import com.smart.car.common.res.exception.BusinessException;
import com.smart.car.resource.entity.ResourceStall;
import com.smart.car.resource.service.RedisService;
import com.smart.car.resource.service.ResourceStallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;

/**
 * 车位资源控制
 *
 * @author zoutao.blog.csdn.net
 * @date: 2021-04-20
 */
@Api(tags = "车位资源模块")
@Slf4j
@RestController
@RequestMapping("stall")
public class ResourceStallController {

    @Autowired
    private ResourceStallService stallService;

    @PostMapping("/list")
    public ResponseResult<List<ResourceStall>> list(String json) throws BusinessException {
        ResponseResult<List<ResourceStall>> commonResult = new ResponseResult<>();
        List<ResourceStall> rtn = stallService.list(json);
        commonResult.setData(rtn);
        return commonResult;
    }

    /**
     * 被使用的车位
     */
    @PostMapping("/occupy")
    public ResponseResult<Integer> occupy(String json) throws BusinessException {
        ResponseResult<Integer> commonResult = new ResponseResult<>();
        int rtn = stallService.occupy(json);
        commonResult.setData(rtn);
        return commonResult;
    }

    /**
     * 车位是否还被占用？
     */
    @PostMapping("/exist")
    public ResponseResult<Integer> exist(String json) throws BusinessException {
        ResponseResult<Integer> commonResult = new ResponseResult<>();
        int rtn = stallService.exist(json);
        commonResult.setData(rtn);
        return commonResult;
    }

    @Autowired
    private RedisService redisService;

    /**
     *  当前可用的停车券
     */
    @PostMapping("/currentAviableStallAmt")
    public ResponseResult<Integer> get() throws BusinessException {
        ResponseResult<Integer> commonResult = new ResponseResult<>();
        Object rtn = redisService.getkey(Constants.cache.currentAviableStallAmt);
        commonResult.setData(Integer.valueOf(rtn + ""));
        return commonResult;
    }


    @Resource
    OSSClient ossClient;

    @Value("${spring.cloud.alicloud.oss.bucket}")
    private String bucketName;

    /**
     * 文件上传到OOS对象存储
     */
    @ApiOperation("上传文件到OSS")
    @PostMapping("/getPolicy")
    public ResponseResult getPolicy(MultipartFile file) {
        if (null == file) {
            return ResponseResult.fail("没有找到相对应的文件");
        }
        String fileKeyName = file.getOriginalFilename();
        String today = DateUtil.today();
        try{
            byte[] bytes = file.getBytes();
            InputStream inputStream = new ByteArrayInputStream(bytes);
            //InputStream inputStream = new FileInputStream(file);
            ossClient.putObject(bucketName, fileKeyName, inputStream);
            ossClient.shutdown();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            ossClient.shutdown();
        }
        return ResponseResult.ok("上传完成"+today);
    }
}
